package com.TIEmulator;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.res.Configuration;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.TIEmulator.JNI.NLib;

public class TIEmulator extends MyAct
  implements OnScreenKeys.OnEnter, EmulatorRenderer.RenderCb
{
  private static final int ID_DIALOG_STARTUP = 0;
  private static final int TIMENU_ID = 2;
  private int mControlKeyCode;
  private View mCursersView;
  private EmulatorRenderer mEmuView = null;
  private final KeyListener mKeyListener = new KeyListener();
  private View mKeysView;
  private AlertDialog mLoadingDialog = null;
  private TextView mLoadingTV = null;
  private View mNumbersView;
  private Button mOnScreenCursorsBtn;
  private Button mOnScreenKeysBtn;
  private Button mOnScreenNumsBtn;
  private LinearLayout on_screen_wrapper;
  private String onscreen_cursors_on_txt;
  private String onscreen_keyboard_off_txt;
  private String onscreen_keys_on_txt;
  private String onscreen_nums_on_txt;
  String upd_msg = "";
  float x;
  float y;

  private boolean handleControlKey(int paramInt, boolean paramBoolean)
  {
    if (paramInt == this.mControlKeyCode)
      this.mKeyListener.handleControlKey(paramBoolean);
    for (int i = 1; ; i = 0)
      return (i == 1);
  }

  public boolean dismissStartupDialog()
  {
    boolean i;
    try
    {
      dismissDialog(0);
      removeDialog(0);
      this.mLoadingDialog = null;
      this.upd_msg = "";
      i = true;
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      i = false;
    }
    return i;
  }

  public void finish()
  {
    Log.v(super.toString(), "on finish!");
    this.mEmuView.onPause();
    this.mEmuView = null;
    super.finish();
  }

  public void onClick(View paramView)
  {
    switch (paramView.getId())
    {
    default:
      this.mOnScreenNumsBtn.setText(this.onscreen_nums_on_txt);
      this.mOnScreenKeysBtn.setText(this.onscreen_keys_on_txt);
      this.mOnScreenCursorsBtn.setText(this.onscreen_cursors_on_txt);
      this.on_screen_wrapper.setVisibility(4);
    case 2131296280:
    case 2131296281:
    case 2131296282:
    }
    while (true)
    {
      if (this.onscreen_keys_on_txt.equals(this.mOnScreenKeysBtn.getText())) {
    	  this.on_screen_wrapper.setVisibility(0);
    	  this.mNumbersView.setVisibility(8);
    	  this.mCursersView.setVisibility(8);
    	  this.mKeysView.setVisibility(0);
    	  this.mOnScreenNumsBtn.setText(this.onscreen_nums_on_txt);
    	  this.mOnScreenCursorsBtn.setText(this.onscreen_cursors_on_txt);
    	  this.mOnScreenKeysBtn.setText(this.onscreen_keyboard_off_txt);
      }
      if (this.onscreen_nums_on_txt.equals(this.mOnScreenNumsBtn.getText())) {
    	  this.on_screen_wrapper.setVisibility(0);
    	  if (this.mKeysView != null)
    		  this.mKeysView.setVisibility(8);
    	  this.mCursersView.setVisibility(8);
    	  this.mNumbersView.setVisibility(0);
    	  this.mOnScreenKeysBtn.setText(this.onscreen_keys_on_txt);
    	  this.mOnScreenCursorsBtn.setText(this.onscreen_cursors_on_txt);
    	  this.mOnScreenNumsBtn.setText(this.onscreen_keyboard_off_txt);
      }
      if (this.onscreen_cursors_on_txt.equals(this.mOnScreenCursorsBtn.getText())) {
    	  this.on_screen_wrapper.setVisibility(0);
    	  this.mNumbersView.setVisibility(8);
    	  if (this.mKeysView != null)
    		  this.mKeysView.setVisibility(8);
    	  this.mCursersView.setVisibility(0);
    	  this.mOnScreenNumsBtn.setText(this.onscreen_nums_on_txt);
    	  this.mOnScreenKeysBtn.setText(this.onscreen_keys_on_txt);
    	  this.mOnScreenCursorsBtn.setText(this.onscreen_keyboard_off_txt);
      }
    }
  }

  public void onConfigurationChanged(Configuration paramConfiguration)
  {
    Log.v("TIEMU:", "Something changed the orientation.. however we don't care");
    super.onConfigurationChanged(paramConfiguration);
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setRequestedOrientation(0);
    setContentView(2130903044);
    GLSurfaceView localGLSurfaceView = (GLSurfaceView)findViewById(2131296278);
    this.mEmuView = new EmulatorRenderer(this, localGLSurfaceView);
    localGLSurfaceView.setRenderer(this.mEmuView);
    localGLSurfaceView.setRenderMode(0);
    Log.v(super.toString(), "===========> GLSurfaceView now is setup!");
    this.on_screen_wrapper = ((LinearLayout)findViewById(2131296279));
    this.onscreen_keys_on_txt = getString(2131165239);
    this.onscreen_nums_on_txt = getString(2131165240);
    this.onscreen_cursors_on_txt = getString(2131165241);
    this.onscreen_keyboard_off_txt = getString(2131165242);
    this.mOnScreenKeysBtn = ((Button)findViewById(2131296280));
    this.mOnScreenNumsBtn = ((Button)findViewById(2131296281));
    this.mOnScreenCursorsBtn = ((Button)findViewById(2131296282));
    this.mOnScreenKeysBtn.setOnClickListener(this);
    this.mOnScreenCursorsBtn.setOnClickListener(this);
    this.mOnScreenNumsBtn.setOnClickListener(this);
    this.mCursersView = View.inflate(this, 2130903041, null);
    this.on_screen_wrapper.addView(this.mCursersView);
    showDialog(0);
  }

  protected Dialog onCreateDialog(int paramInt)
  {
    if (paramInt == 0)
    {
      ScrollView localScrollView = new ScrollView(this);
      this.mLoadingTV = new TextView(this);
      localScrollView.addView(this.mLoadingTV);
      this.mLoadingTV.setText(this.upd_msg);
      String str = getString(2131165208);
      AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
      localBuilder.setView(localScrollView);
      localBuilder.setTitle(str);
      localBuilder.setCancelable(false);
      this.mLoadingDialog = localBuilder.create();
    }
    for (Object localObject = this.mLoadingDialog; ; localObject = super.onCreateDialog(paramInt))
      return (Dialog)localObject;
  }

  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    paramMenu.add(0, 2, 0, 2131165233);
    return super.onCreateOptionsMenu(paramMenu);
  }

  public void onDestroy()
  {
    super.onDestroy();
  }

  public void onEnterClicked()
  {
    this.on_screen_wrapper.setVisibility(4);
    this.mOnScreenNumsBtn.setText(this.onscreen_nums_on_txt);
    this.mOnScreenKeysBtn.setText(this.onscreen_keys_on_txt);
    this.mOnScreenCursorsBtn.setText(this.onscreen_cursors_on_txt);
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    boolean bool1 = false;
    if (!(handleControlKey(paramInt, true)));
    switch (paramInt)
    {
    default:
      label48: bool1 = paramKeyEvent.isSystem();
      if (bool1)
        break label131;
      int[] arrayOfInt = this.mKeyListener.keyDown(paramInt, paramKeyEvent);
      if ((arrayOfInt == null) || (arrayOfInt[0] <= -1))
        break label131;
      NLib.postIntermediateKeyCode(arrayOfInt[0], arrayOfInt[1]);
    case 84:
    case 82:
    case 4:
    }
    for (boolean bool2 = true; ; bool2 = super.onKeyDown(paramInt, paramKeyEvent))
    {
      while (true)
      {
        return bool2;
        NLib.postTIKeyCodeIdx(OnScreenKeys.SECOND_KC);
        bool2 = true;
        continue;
        bool2 = super.onKeyDown(paramInt, paramKeyEvent);
      }
      finish();
      label131: break label48:
    }
  }

  public boolean onKeyUp(int paramInt, KeyEvent paramKeyEvent)
  {
    if (handleControlKey(paramInt, false));
    for (boolean bool = true; ; bool = true)
    {
      while (true)
      {
        if (!(paramKeyEvent.isSystem()))
          break;
        bool = super.onKeyUp(paramInt, paramKeyEvent);
      }
      this.mKeyListener.keyUp(paramInt);
      return bool;
    }
  }

  public boolean onOptionsItemSelected(MenuItem paramMenuItem)
  {
    switch (paramMenuItem.getItemId())
    {
    default:
    case 2:
    }
    for (boolean bool = super.onOptionsItemSelected(paramMenuItem); ; bool = true)
    {
      NLib.postTIKeyCodeIdx(OnScreenKeys.APPS_KC);
      return bool;
    }
  }

  public void onPause()
  {
    super.onPause();
    if (this.mLoadingDialog != null)
      removeDialog(0);
    this.mLoadingDialog = null;
    this.mEmuView.onPause();
  }

  public void onResume()
  {
    super.onResume();
    this.mEmuView.onResume();
    String str = NLib.querryRomType();
    if ((str.equals("TI92")) || (str.equals("TI92p")) || (str.equals("V200")))
    {
      this.mKeysView = View.inflate(this, 2130903046, null);
      this.mNumbersView = View.inflate(this, 2130903047, null);
      this.on_screen_wrapper.addView(this.mKeysView);
      this.on_screen_wrapper.addView(this.mNumbersView);
      OnScreenFKeys.Setup(findViewById(2131296277), this, -1);
    }
    while (true)
    {
      OnScreenKeys.Setup(this.on_screen_wrapper, this);
      if (this.mKeysView != null)
        this.mKeysView.setVisibility(8);
      this.mNumbersView.setVisibility(8);
      this.mCursersView.setVisibility(8);
      return;
      this.mOnScreenKeysBtn.setVisibility(4);
      this.mNumbersView = View.inflate(this, 2130903048, null);
      OnScreenFKeys.Setup(findViewById(2131296277), this, 5);
      this.on_screen_wrapper.addView(this.mNumbersView);
    }
  }

  public boolean onTrackballEvent(MotionEvent paramMotionEvent)
  {
    switch (paramMotionEvent.getAction())
    {
    case 1:
    default:
    case 2:
    case 0:
    }
    for (boolean bool = super.onTrackballEvent(paramMotionEvent); ; bool = true)
    {
      label38: return bool;
      this.x += paramMotionEvent.getX();
      this.y += paramMotionEvent.getY();
      if (this.x < -1.0F)
      {
        NLib.postTIKeyCodeIdx(OnScreenKeys.LEFT_KC);
        this.x = 0.0F;
        this.y = 0.0F;
        label98: if (this.y >= -1.0F)
          break label170;
        NLib.postTIKeyCodeIdx(OnScreenKeys.UP_KC);
        this.x = 0.0F;
      }
      for (this.y = 0.0F; ; this.y = 0.0F)
      {
        do
        {
          bool = true;
          break label38:
          if (this.x > 1.0F);
          NLib.postTIKeyCodeIdx(OnScreenKeys.RIGHT_KC);
          this.x = 0.0F;
          this.y = 0.0F;
          label170: break label98:
        }
        while (this.y <= 1.0F);
        NLib.postTIKeyCodeIdx(OnScreenKeys.DOWN_KC);
        this.x = 0.0F;
      }
      NLib.postTIKeyCodeIdx(OnScreenKeys.ENTER_KC);
    }
  }

  public void updateStartupDialog(String paramString)
  {
    this.upd_msg = this.upd_msg + paramString + "\n";
    if (this.mLoadingTV == null)
      return;
    this.mLoadingTV.setText(this.upd_msg);
  }
}
